package org.waldev.multi;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class Multitouch extends Activity{
	private Table game;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		final Window win = getWindow();
		super.onCreate(savedInstanceState);
		game = new Table(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(game);
	}
	public void onPause()
	{
		super.onPause();
		finish();
	}
	public class Table extends SurfaceView implements SurfaceHolder.Callback
	{
		Threads thread;
		Paint paint = new Paint();
		Point[] touchPlaces = new Point[0];
		int ballX = 0, ballY = 0;

		public Table(Context context) {
			super(context);
			getHolder().addCallback(this);
			thread = new Threads(getHolder(), this);
			setFocusable(true);
		}
		public int getColor(int index)
		{
			int color;
			if(index == 0)
			{
				color = Color.BLUE;
			}
			else if(index == 1)
			{
				color = Color.CYAN;
			}
			else if(index == 2)
			{
				color = Color.RED;
			}
			else if(index == 3)
			{
				color = Color.rgb(0, 80, 200);
			}
			else if(index == 4)
			{
				color = Color.GREEN;
			}
			else if(index == 5)
			{
				color = Color.YELLOW;
			}
			else if(index == 6)
			{
				color = Color.WHITE;
			}
			else if(index == 7)
			{
				color = Color.LTGRAY;
			}
			else if(index == 8)
			{
				color = Color.DKGRAY;
			}
			else
			{
				color = Color.MAGENTA;
			}
			return color;
		}
		public void setStates()
		{

		}
		public void onDraw(Canvas c)
		{
			c.drawColor(Color.BLACK);
			Point[] touchPlacesHolder = touchPlaces;
			for(int i = 0; i < touchPlacesHolder.length; i++)
			{
				paint.setColor(getColor(i));
				c.drawCircle(touchPlacesHolder[i].x, touchPlacesHolder[i].y, 100, paint);
			}
		}

		public boolean onTouchEvent(MotionEvent event){
			touchPlaces = new Point[event.getPointerCount()];
			for (int i = 0; i < event.getPointerCount(); i++) 
			{
				touchPlaces[i] = new Point((int) event.getX(i), (int) event.getY(i));
			}
			return true;
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub

		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			thread.setRunning(true);
			thread.start();
		}

		//if its destroyed, then destroy the thread
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			boolean retry = true;
			thread.setRunning(false);
			while (retry) {
				try {
					thread.join();
					retry = false;
				} catch (InterruptedException e) {
					// we will try it again and again...
				}
			}
		}
	}
}
